Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tutorial: tensor network basics #1193

Open
wants to merge 55 commits into
base: master
Choose a base branch
from
Open

Tutorial: tensor network basics #1193

wants to merge 55 commits into from

Conversation

EmilianoG-byte
Copy link
Collaborator

@EmilianoG-byte EmilianoG-byte commented Aug 16, 2024

Before submitting

Please complete the following checklist when submitting a PR:

  • Ensure that your tutorial executes correctly, and conforms to the
    guidelines specified in the README.

  • Remember to do a grammar check of the content you include.

  • All tutorials conform to
    PEP8 standards.
    To auto format files, simply pip install black, and then
    run black -l 100 path/to/file.py.

When all the above are checked, delete everything above the dashed
line and fill in the pull request template.


Title:

Summary:

Relevant references:

[sc-66746]


If you are writing a demonstration, please answer these questions to facilitate the marketing process.

  • GOALS — Why are we working on this now?

    Eg. Promote a new PL feature or show a PL implementation of a recent paper.

  • AUDIENCE — Who is this for?

    Eg. Chemistry researchers, PL educators, beginners in quantum computing.

  • KEYWORDS — What words should be included in the marketing post?

  • Which of the following types of documentation is most similar to your file?
    (more details here)

  • Tutorial
  • Demo
  • How-to

Copy link

👋 Hey, looks like you've updated some demos!

🐘 Don't forget to update the dateOfLastModification in the associated metadata files so your changes are reflected in Glass Onion (search and recommendations).

Please hide this comment once the field(s) are updated. Thanks!

Copy link
Collaborator

@Qottmann Qottmann left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great skeleton @EmilianoG-byte @Shiro-Raven !

Mainly minor suggestions, and one bigger one towards the last section for quantum circuit applications

demonstrations/tutorial_tn_basics.py Outdated Show resolved Hide resolved
demonstrations/tutorial_tn_basics.py Outdated Show resolved Hide resolved
demonstrations/tutorial_tn_basics.py Outdated Show resolved Hide resolved
demonstrations/tutorial_tn_basics.py Outdated Show resolved Hide resolved
demonstrations/tutorial_tn_basics.py Outdated Show resolved Hide resolved
demonstrations/tutorial_tn_basics.py Outdated Show resolved Hide resolved
demonstrations/tutorial_tn_basics.py Outdated Show resolved Hide resolved
demonstrations/tutorial_tn_basics.py Outdated Show resolved Hide resolved
demonstrations/tutorial_tn_basics.py Outdated Show resolved Hide resolved
demonstrations/tutorial_tn_basics.py Outdated Show resolved Hide resolved
@EmilianoG-byte EmilianoG-byte marked this pull request as ready for review August 21, 2024 21:34
Copy link

github-actions bot commented Aug 21, 2024

Thank you for opening this pull request.

You can find the built site at this link.

Deployment Info:

  • Pull Request ID: 1193
  • Deployment SHA: 8a6f757df2d172124a4d7db375deda638038a5a7
    (The Deployment SHA refers to the latest commit hash the docs were built from)

Note: It may take several minutes for updates to this pull request to be reflected on the deployed site.

@EmilianoG-byte
Copy link
Collaborator Author

EmilianoG-byte commented Aug 30, 2024

Initial draft from last week:

DRAFT:

  • Definition of a tensor as an n-dimensional array. Show notation with n indices, stating that it belongs to C^{d1,...,dn}. Define rank, index, dimension (mention how these terms are sometimes used (wrongly?) interchangeably in literature).
  • Graphical notation. Mention that there exist certain representations in the literature that allow to represent properties of the tensors (e.g. symmetry, orthogonality). In our case, we can adhere to a general circle.
  • Specific examples used in a day-to-day: scalars, vectors, matrices. Mention that for quantum states, we can adopt the convention that the legs in one direction mean that the state belongs to one Hilbert space, and the legs to the other side to the dual space.
  • CODE: include code using numpy creating a >2 dimensional array.
  • Show the matrix multiplication in terms of summation over indices, then using the diagrammatic representation. This results in another rank 2 tensor (matrix)
  • Analagously, we can represent matrix-vector multiplication resulting in a rank 1 tensor (vector). Just as we expected!
  • We can generalize this concept to tensors. This is done by summing over repeated indices (just as in einstein convention - external link for it) resulting in another tensor made up of the open legs of all the tensors together.
    In diagrammatic notation, this is simply sticking together legs with same indices! (show nice diagram with >3 tensors). We have just formed a network of tensors, i.e. a Tensor Network!
  • CODE: Talking about einstein convetion, we can perform this contraction of tensors using np.einsum.
  • Mention that the resulting tensor network doesn't change but the way we arrive to the final tensor affects how expensive it is to get there.
  • Show how can we can calculate the complexity of a contraction by means of a simple example using 2 matrices (rank 2 tensors): dimension_contracted x (dimensions_open).
  • Intuition behind: we perform one operation (contraction) and repeat many times to "populate" the resulting tensor (dimension_open1 x dimension_open2). Show the equation with indices.
  • Show an example with at least three tensors where they all have different dimensions. Walk through it showing that choosing to contract two indices (the ones with lower dimensions) results in a worst computational complexity than contracting other ones (the ones with higher dimensions).

Very nice source with visual explanations that we can cite: https://www.math3ma.com/blog/matrices-as-tensor-network-diagrams

@EmilianoG-byte
Copy link
Collaborator Author

Hi @Qottmann ! I have finished checking the spelling and grammar so I believe the demo is now ready for a review :).

If anything, I saw my last 3 drawings could use some improvement in the thickness of the lines, but I guess that’s a minor detail I can correct in the following days :D

@Qottmann
Copy link
Collaborator

@EmilianoG-byte awesome :) you can expect a review at the latest by eow

Copy link
Collaborator

@Qottmann Qottmann left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great first draft @EmilianoG-byte , congrats!

I left a bunch of nitpicky comments, in particular feel free to ignore those marked as "personal opinion" at your discretion.

The demo starts relatively slow in the beginning (which is great pedagogically) and then very quickly goes very fast (also understandable since that is in the nature of these highly complex topics).

I wonder if you can adjust the pace on either ends to make the experience smoother (I understand this is a very vague and hard-to-implement suggestion, but perhaps you get an idea). Perhaps it is also more a matter of the framing of the scope of the demo, making it clear in the beginning, end and thoughout what this demo is trying to achieve.

Perhaps as a good exercise for you to answer first and then use to translate into the draft: who is the target audience of this demo? what is the intention of writing this demo? and what should a reader take away from it?

I think the content itself it already great, it is just a matter of framing and scoping of the text :)


Part of the excitement surrounding tensor networks is due to their ability to represent complex data efficiently, which allows for — among other things — fast classical simulations. In addition, the diagrammatic language accompanying tensor networks makes working with them intuitive and suitable for describing a vast range of mathematical concepts, including quantum circuits.

In this tutorial, we aim to provide an introduction to tensor networks with a focus on their applications in quantum computing. We choose to start by discussing the basic notions and definitions of tensors and tensor networks and work our way up to more advanced topics such as contraction paths and algorithms used to simulate quantum computers using tensor networks.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(personal opinion)
could be reduced or even removed

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should I make the changes you were referring about being more clear about the scope of the demo?

Copy link
Collaborator Author

@EmilianoG-byte EmilianoG-byte Nov 22, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have added some transition sentences (and even an intermezzo :o), let me know what you think @Qottmann !

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I quite like the idea of the intermezzo :) from reading that the context is clear. do you think there is also a way you could specify in the intro which parts of the contents are going to be "the first part"? Because technically this currently consists of 3 sub-parts, perhaps you can introduce main headings "part1" and "part 2", with the current headings (minus intermezzo) being sub-headings.

In particular, this structure

1. Part 1
1.1 from matrices to tensors
1.2 from matrix multiplication to tensor contractions
1.2.1 CNOT gate
1.3 cost of contraction
2. Intermezzo
3. Part 2
...

(note that I dont mean that the headings in the content actually have this numbering, this is just to explain the logic)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the suggestion! I have added an extra level of headers and reference the main two parts in the intro but it does not seem to be deploying correctly for some reason 🤔

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah sphinx is odd sometimes, I'd suggest to check other demos that reference sections and see how they do it

Copy link
Collaborator Author

@EmilianoG-byte EmilianoG-byte Dec 2, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's solved :) @Qottmann

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think @ikurecic fixed it by merging the main branch, thank you! 🙏

demonstrations/tutorial_tn_basics.py Show resolved Hide resolved
demonstrations/tutorial_tn_basics.py Outdated Show resolved Hide resolved

Does the last diagram seem familiar? It is because this is the representation of a single-qubit gate! We will see later in this tutorial the relation between quantum circuits and tensor networks.

When working within the quantum computing notation, we adopt the convention that drawing the leg of a quantum state (i.e., a vector) to the right corresponds to a ket, i.e., a vector living in the Hilbert space, while drawing the legs to the left means they are a bra vector, i.e., living in the dual space.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the differentiation between bra and kets via the direction they are pointing a common definition and actually necessary here? Instead of ascribing duals to left-pointing legs, you can also just indicate complex conjugation of $v$ as the concept of transposition becomes irrelevant in tensor diagrams (leaving only complex conjugation)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would say for "general tensor networks" (like in condensed matter) this is the opposite convention. But since in quantum computing we read from left to right, you can see that all kets result in legs pointing right. Analogously the bra $\langle v|$ is a tensor with legs pointing left. Again, this is probably just the convention that arisis in quantum circuit circuit diagrams and is not a general convention. I am not sure i understand the second part of your comment 🤔

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

when writing tensor diagrams, transposition becomes irrelevant as it is always clear from context. So the only thing you need to differentiate a bra from a ket is complex conjugation, which you can indicate with a star or bar over the tensor name :)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I know what you refer to, but I don't think those statements agree with these definitions:

Screenshot 2024-11-24 at 17 50 10

from https://arxiv.org/pdf/1912.10049. When thinking of the applications I have encountered in condensed matter I have the impressions that your statements make sense. But in other applications, representation the transposition of a tensor by bending its wires has been quite fundamental (in my experience).

Copy link
Collaborator

@Qottmann Qottmann Nov 25, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I personally have never seen these definitions and conventions outside of this particular review by Biamonte. There is nothing wrong with it, I was just wondering if it's adding something to your demo or making things unnecessarily comlicated (as in, are you making good use of this in the demo? Or can the demo do without it? I dont think it's widespread and therefore necessary to share as common knowledge). I'm sharing this observation and in the end it is up to you as the author to decide :)

demonstrations/tutorial_tn_basics.py Outdated Show resolved Hide resolved
demonstrations/tutorial_tn_basics.py Outdated Show resolved Hide resolved
demonstrations/tutorial_tn_basics.py Show resolved Hide resolved
demonstrations/tutorial_tn_basics.py Show resolved Hide resolved
demonstrations/tutorial_tn_basics.py Show resolved Hide resolved
demonstrations/tutorial_tn_basics.py Outdated Show resolved Hide resolved
Comment on lines +11 to +12
"dateOfPublication": "2024-08-06T00:00:00+00:00",
"dateOfLastModification": "2024-08-06T00:00:00+00:00",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tk

correct explanation on the definition of contraction
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants